package com.ecoolex.ying.space.core.manager;

import java.time.Instant;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.common.util.CollectorUtil;
import com.ecoolex.ying.space.core.dao.UserWorksOpusItemsDAO;
import com.ecoolex.ying.space.entity.UserWorks;
import com.ecoolex.ying.space.entity.UserWorksOpusItems;
import com.ecoolex.ying.space.entity.UserWorksOpusItems.Column;
import com.ecoolex.ying.space.entity.UserWorksOpusItemsExample;
import com.ecoolex.ying.space.entity.UserWorksOpusItemsExample.Criteria;
import com.ecoolex.ying.space.pojo.AddUserWorksImagesDTO;
import com.ecoolex.ying.space.pojo.AddUserWorksOpusItemsDTO;

@Service
public class UserWorksOpusItemsManager {
	
    @Autowired
    private UserWorksOpusItemsDAO dao;
    
    @Autowired
    private UserWorksShoppingManager userWorksShoppingManager;
    
    public void verify(AddUserWorksOpusItemsDTO dto) {
    	BizAssert.isTrue(Check.notNullOrTrimEmpty(dto.getName()), ResultCode.VERIFY.build(10), "作品栏目名称为空");
    }
    
    public void updateUserWorksOpusItems(UserWorks userWorks,List<AddUserWorksOpusItemsDTO> addUserWorksOpusItemsDTOList,String createUser) {
    	List<UserWorksOpusItems> oldList = getByWorksId(userWorks.getWorksId());
    	for(int i = 0;i<addUserWorksOpusItemsDTOList.size();i++) {
    		AddUserWorksOpusItemsDTO dto = addUserWorksOpusItemsDTOList.get(i);
    		verify(dto);
    		//修改
    		if(Check.notNull(dto.getItemId())) {
    			UserWorksOpusItems old = oldList.stream().collect(CollectorUtil.first(o -> dto.getItemId().equals(o.getItemId())));
    			BizAssert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(30),"作品栏目id={}不存在",dto.getItemId());
    			old.setName(dto.getName());
    			old.setOrdered(i+1);
    			old.setDescription(dto.getDescription());
    			List<AddUserWorksImagesDTO> addUserWorksImagesDTOList = dto.getAddUserWorksImagesDTOList();
    			JSONArray jsonArray = new JSONArray();
    			for (AddUserWorksImagesDTO addUserWorksImagesDTO : addUserWorksImagesDTOList) {
					jsonArray.add(addUserWorksImagesDTO.getStorageId());
					//处理作品购物
					userWorksShoppingManager.updateUserWorksShopping(userWorks, addUserWorksImagesDTO.getStorageId(), addUserWorksImagesDTO.getAddUserWorksShoppingsDTOList(), createUser);
				}
    			old.setImages(jsonArray.toString());
    			old.setUpdateUser(createUser);
    			old.setUpdateTime(Date.from(Instant.now()));
    			dao.updateByPrimaryKeySelective(old, Column.excludes(Column.worksId,Column.createUser,Column.createTime));
    			
    			oldList.remove(old);
    		}else {//新增
    			UserWorksOpusItems userWorksOpusItems = new UserWorksOpusItems();
    			userWorksOpusItems.setWorksId(userWorks.getWorksId());
    			userWorksOpusItems.setName(dto.getName());
    			userWorksOpusItems.setOrdered(i+1);
    			userWorksOpusItems.setDescription(dto.getDescription());
    			List<AddUserWorksImagesDTO> addUserWorksImagesDTOList = dto.getAddUserWorksImagesDTOList();
    			JSONArray jsonArray = new JSONArray();
    			for (AddUserWorksImagesDTO addUserWorksImagesDTO : addUserWorksImagesDTOList) {
					jsonArray.add(addUserWorksImagesDTO.getStorageId());
					//处理作品购物
					userWorksShoppingManager.updateUserWorksShopping(userWorks, addUserWorksImagesDTO.getStorageId(), addUserWorksImagesDTO.getAddUserWorksShoppingsDTOList(), createUser);
				}
    			userWorksOpusItems.setImages(jsonArray.toString());
    			userWorksOpusItems.setDeleted(UserWorksOpusItems.NOT_DELETED);
    			userWorksOpusItems.setUpdateUser(createUser);
    			userWorksOpusItems.setUpdateTime(Date.from(Instant.now()));
    			userWorksOpusItems.setCreateUser(createUser);
    			userWorksOpusItems.setCreateTime(Date.from(Instant.now()));
    			dao.insert(userWorksOpusItems);
    		}
    	}
    	// 删除多余的
    	oldList.forEach(old -> delete(old, createUser));
    }
    
    public List<UserWorksOpusItems> getByWorksId(Integer worksId) {
    	UserWorksOpusItemsExample example = new UserWorksOpusItemsExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andWorksIdEqualTo(worksId).andDeletedEqualTo(UserWorksOpusItems.NOT_DELETED);
    	example.setOrderByClause(Column.ordered.asc());
    	return dao.selectByExample(example);
    }
    
    public void delete(UserWorksOpusItems record,String updateUser) {
    	record.setDeleted(UserWorksOpusItems.IS_DELETED);
    	record.setUpdateUser(updateUser);
    	record.setUpdateTime(Date.from(Instant.now()));
    	dao.updateByPrimaryKeySelective(record, Column.deleted,Column.updateUser,Column.updateTime);
    }
    
    public void deleteByWorksId(UserWorks userWorks, String updateUser) {
    	UserWorksOpusItemsExample example = new UserWorksOpusItemsExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andWorksIdEqualTo(userWorks.getWorksId()).andDeletedEqualTo(UserWorksOpusItems.NOT_DELETED);
    	List<UserWorksOpusItems> list = dao.selectByExample(example);
    	if(Check.notNullOrEmpty(list)) {
    		for (UserWorksOpusItems userWorksOpusItems : list) {
    			userWorksOpusItems.setDeleted(UserWorksOpusItems.IS_DELETED);
    			userWorksOpusItems.setUpdateUser(updateUser);
    			userWorksOpusItems.setUpdateTime(Date.from(Instant.now()));
    			dao.updateByPrimaryKeySelective(userWorksOpusItems, Column.deleted,Column.updateUser,Column.updateTime);
    			userWorksShoppingManager.deleteByWorksId(userWorks.getWorksId(), updateUser);
			}
    	}
    }
}